package com.fs.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.SysLogMapper;
import com.fs.pojo.SysLogs;
import com.fs.vo.PageResult;
import com.fs.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogMapper sysLogMapper;

    @Override
    @Transactional
    public SysResult add(SysLogs entity) {
        sysLogMapper.insert(entity);
        return SysResult.success();
    }

    @Override
    @Transactional
    public SysResult delById(Integer id) {
        sysLogMapper.deleteById(id);
        return SysResult.success();
    }

    @Override
    public SysResult list(PageResult request) {
        if(request==null)return SysResult.fail();

        int pageNum = request.getPageNum();
        int pageSize = request.getPageSize();

        if(pageNum<1)pageNum=1;
        if(pageSize<1)pageNum=10;

        Page<SysLogs> page = new Page<>(pageNum, pageSize);


        QueryWrapper<SysLogs> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(request.getQuery()),"username",request.getQuery())
                .or()
                .like(StrUtil.isNotBlank(request.getQuery()),"operation",request.getQuery())
                .orderByDesc("id");
        page=sysLogMapper.selectPage(page,queryWrapper);
        request.setTotal(page.getTotal()).setRows(page.getRecords());

        return SysResult.success(request);
    }

    @Override
    @Transactional
    public SysResult edit(SysLogs entity) {
        sysLogMapper.updateById(entity);
        return SysResult.success();
    }
}
